Death to xml_fill_in_time with its weird interface. Replace with
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 7 Jul 2013 23:56:36 +0000 (23:56 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 7 Jul 2013 23:56:36 +0000 (23:56 +0000)
PrettyPrinter in our DateTime except for coastexp which does its own
thing.

gpsbabel/coastexp.cc
gpsbabel/csv_util.cc
gpsbabel/gpx.cc
gpsbabel/gtrnctr.cc
gpsbabel/kml.cc
gpsbabel/src/core/datetime.h
gpsbabel/xmlgeneric.cc
gpsbabel/xmlgeneric.h

index cadae77b93e27ed792a6c8c69352ba8f0f8024e4..0ac07e8cda1928060805d7d055680e0612711553 100644 (file)
@@ -65,7 +65,6 @@ static queue ce_route_head;                           // List of routes currently found
 static ce_route* currentRoute = NULL;  // Current route being processed
 static queue ce_mark_head;                             // List of stand-alone marks currently found
 static ce_mark* currentMark = NULL;            // Current mark being processed
-static char* time_buffer = NULL;               // Time buffer for processing times
 static char* uuid_buffer = NULL;               // UUID buffer for processing uuid's
 static char* xml_buffer = NULL;                        // XML buffer for processing XML strings
 static int inRoute = 0;                                        // Are we processing a route?
@@ -501,7 +500,6 @@ ce_wr_init(const char* fname)
   QUEUE_INIT(&ce_mark_head);
 
   // Alloocate all buffers used for writing
-  time_buffer = (char*) xcalloc(MY_TBUF,1);
   uuid_buffer = (char*) xcalloc(MY_UBUF,1);
   xml_buffer = (char*) xcalloc(MY_XBUF, 1);
 
@@ -515,23 +513,21 @@ ce_wr_deinit(void)
   gbfclose(ofd);
 
   // Free the buffers used for writing
-  xfree(time_buffer);
   xfree(uuid_buffer);
   xfree(xml_buffer);
 }
 
 /* Generate a CE-style creation time based on supplied time */
-static char*
+static QString
 ce_gen_creation_time(time_t tm)
 {
   QDateTime qtm;
   qtm = QDateTime::fromTime_t(tm);
-  xml_fill_in_time(time_buffer, qtm, XML_SHORT_TIME);
-  return time_buffer;
+  return qtm.toUTC().toString("yyyyMMddTHHmmssZ");
 }
 
 /* Generate a CE-style creation time based on current time */
-static char*
+static QString
 ce_gen_current_time(void)
 {
   return ce_gen_creation_time(current_time());
index 56eedd332bafe70cff4fb7b99401fb0c65f9639e..c392813074ffb2410084cf4f3e541d94d39358ad 100644 (file)
@@ -28,7 +28,6 @@
 #include "grtcirc.h"
 #include "strptime.h"
 #include "jeeps/gpsmath.h"
-#include "xmlgeneric.h"  // for xml_fill_in_time.
 #include "garmin_fs.h"
 
 #define MYNAME "CSV_UTIL"
@@ -913,7 +912,7 @@ addhms(const char* s, const char* format)
 
 static
 int
-writetime(char* buff, size_t bufsize, const char* format, time_t t, int gmt)
+writetime(char* buff, size_t bufsize, const char* format, time_t t, bool gmt)
 {
   static struct tm* stmp;
 
@@ -922,7 +921,6 @@ writetime(char* buff, size_t bufsize, const char* format, time_t t, int gmt)
   } else {
     stmp = localtime(&t);
   }
-
   return strftime(buff, bufsize, format, stmp);
 }
 
@@ -1946,7 +1944,7 @@ xcsv_waypt_pr(const waypoint* wpt)
     case XT_TIMET_TIME_MS: {
       /* time as a time_t variable in milliseconds */
       char tbuf[24];
-      writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime(), 0);
+      writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime(), false);
       char mbuf[32];
       snprintf(mbuf, sizeof(mbuf), "%s%03d", tbuf, wpt->microseconds / 1000);
       writebuff(buff, "%s", mbuf);
@@ -1956,10 +1954,10 @@ xcsv_waypt_pr(const waypoint* wpt)
       writebuff(buff, fmp->printfc, time_to_yyyymmdd(wpt->GetCreationTime()));
       break;
     case XT_GMT_TIME:
-      writetime(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), 1);
+      writetime(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), true);
       break;
     case XT_LOCAL_TIME:
-      writetime(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), 0);
+      writetime(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), false);
       break;
     case XT_HMSG_TIME:
       writehms(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), 1);
@@ -1968,10 +1966,10 @@ xcsv_waypt_pr(const waypoint* wpt)
       writehms(buff, sizeof buff, fmp->printfc, wpt->GetCreationTime(), 0);
       break;
     case XT_ISO_TIME:
-      writetime(buff, sizeof buff, "%Y-%m-%dT%H:%M:%SZ", wpt->GetCreationTime(), 1);
+      writetime(buff, sizeof buff, "%Y-%m-%dT%H:%M:%SZ", wpt->GetCreationTime(), true);
       break;
     case XT_ISO_TIME_MS:
-      xml_fill_in_time(buff, wpt->GetCreationTime(), XML_LONG_TIME);
+        strcpy(buff, wpt->GetCreationTime().toPrettyString().toUtf8().data());
       break;
     case XT_GEOCACHE_LAST_FOUND:
       writebuff(buff, fmp->printfc, time_to_yyyymmdd(wpt->gc_data->last_found));
index cb98261323ede9651deafa080c56624d39dbe737..f93ba139f1837e446e156fd2226d15f3d593393f 100644 (file)
@@ -20,7 +20,6 @@
  */
 
 #include "defs.h"
-#include "xmlgeneric.h"
 #include "cet_util.h"
 #include "garmin_fs.h"
 #if HAVE_LIBEXPAT
@@ -1440,12 +1439,8 @@ fprint_xml_chain(xml_tag* tag, const waypoint* wpt)
       }
       if (wpt && wpt->gc_data->exported &&
           strcmp(tag->tagname, "groundspeak:cache") == 0) {
-        char time_string[64];
-        xml_fill_in_time(time_string, wpt->gc_data->exported,
-                         XML_LONG_TIME);
-        if (time_string[0]) {
-          writer.writeTextElement("time", time_string);
-        }
+          writer.writeTextElement("time", 
+                                  wpt->gc_data->exported.toPrettyString());
       }
       writer.writeEndElement();
     }
@@ -1941,11 +1936,8 @@ gpx_write(void)
   }
 
   gpsbabel::DateTime now = current_time();
-  char time_string[64];
-  xml_fill_in_time(time_string, now, XML_LONG_TIME);
-  if (time_string[0]) {
-    writer.writeTextElement("time", time_string);
-  }
+  writer.writeTextElement("time", now.toPrettyString());
+
   gpx_write_gdata(&gpx_global->keywords, "keywords");
 
   gpx_write_bounds();
index ba9460472bb2f4bf96c176a0d2c4e9960ffb1ef8..9d43fa3fc20afb38f2fe2ba06371502b578b8f09 100644 (file)
@@ -267,7 +267,7 @@ gtc_study_lap(const waypoint* wpt)
   }
 
   if (wpt->creation_time && (gtc_least_time > wpt->GetCreationTime())) {
-    gtc_least_time =  wpt->GetCreationTime();
+    gtc_least_time = wpt->GetCreationTime();
     gtc_start_lat = wpt->latitude;
     gtc_start_long = wpt->longitude;
   }
@@ -391,8 +391,7 @@ gtc_act_hdr(const route_head* rte)
   gtc_new_study_lap(rte);
   route_disp(rte, gtc_study_lap);
   if (gtc_least_time) {
-    char time_string[100];
-    xml_fill_in_time(time_string, gtc_least_time, XML_LONG_TIME);
+    const char* time_string = gtc_least_time.toPrettyString().toUtf8().data();
     gtc_write_xml(0, "<Id>%s</Id>\n", time_string);
     gtc_write_xml(1, "<Lap StartTime=\"%s\">\n", time_string);
   } else {
index cbc29ab5fc90c9807a95677c017d4426d2d6d931..c4dc0ab30ebf51a7358e5fa5f7182aa85682bc77 100644 (file)
@@ -712,12 +712,15 @@ void kml_output_trkdescription(const route_head* header, computed_trkdata* td)
     kml_td(hwriter, "Max Cadence", QString(" %1 rpm ").arg(QString::number(td->max_cad)));
   }
   if (td->start && td->end) {
-    char time_string[64];
-    // FIXME (robertl): this straddling of time and string types is killing me
-    xml_fill_in_time(time_string, QDateTime::fromTime_t(td->start), XML_LONG_TIME);
-    kml_td(hwriter, "Start Time", QString(" %1 ").arg(time_string));
-    xml_fill_in_time(time_string, QDateTime::fromTime_t(td->end), XML_LONG_TIME);
-    kml_td(hwriter, "End Time", QString(" %1 ").arg(time_string));
+    gpsbabel::DateTime t;
+    t = td->start;
+    if (t.isValid()) {
+      kml_td(hwriter, "Start Time", t.toPrettyString());
+    }
+    t = td->end;
+    if (t.isValid()) {
+      kml_td(hwriter, "End Time", t.toPrettyString());
+    }
   }
 
   hwriter.writeCharacters("\n");
@@ -730,13 +733,14 @@ void kml_output_trkdescription(const route_head* header, computed_trkdata* td)
 
   /* We won't always have times. Garmin saved tracks, for example... */
   if (td->start && td->end) {
-    char time_string[64];
     writer->writeStartElement("TimeSpan");
-    // FIXME (robertl): this straddling of time and string types is gross
-    xml_fill_in_time(time_string, QDateTime::fromTime_t(td->start), XML_LONG_TIME);
-    writer->writeTextElement("begin", time_string);
-    xml_fill_in_time(time_string, QDateTime::fromTime_t(td->end), XML_LONG_TIME);
-    writer->writeTextElement("end", time_string);
+
+    gpsbabel::DateTime t;
+    t = td->start;
+    writer->writeTextElement("begin", t.toPrettyString());
+    t = td->end;
+    writer->writeTextElement("end", t.toPrettyString());
+
     writer->writeEndElement(); // Close TimeSpan tag
   }
 }
@@ -1841,15 +1845,10 @@ void kml_write_AbstractView(void)
 
   if (kml_time_min || kml_time_max) {
     writer->writeStartElement("gx:TimeSpan");
-    if (kml_time_min) {
-      char time_string[64];
-      xml_fill_in_time(time_string, kml_time_min, XML_LONG_TIME);
-      if (time_string[0]) {
-        writer->writeTextElement("begin", time_string);
-      }
+    if (kml_time_min.isValid()) {
+      writer->writeTextElement("begin", kml_time_min.toPrettyString());
     }
     if (kml_time_max) {
-      char time_string[64];
       gpsbabel::DateTime time_max;
       // In realtime tracking mode, we fudge the end time by a few minutes
       // to ensure that the freshest data (our current location) is contained
@@ -1859,10 +1858,7 @@ void kml_write_AbstractView(void)
       // ensure the right edge of that time slider includes us.
       //
       time_max = realtime_positioning ? kml_time_max + 600 : kml_time_max;
-      xml_fill_in_time(time_string, time_max, XML_LONG_TIME);
-      if (time_string[0]) {
-        writer->writeTextElement("end", time_string);
-      }
+      writer->writeTextElement("end", time_max.toPrettyString());
     }
     writer->writeEndElement(); // Close gx:TimeSpan tag
   }
index 4456d18d44deeffcfd7a94eb95d8387541e67403..4cab6842898c188ff21658f437f40e8c95bf8d42 100644 (file)
@@ -104,6 +104,19 @@ public:
     QTime time(this->time());
     return time.hour() * 10000 + time.minute() * 100 + time.second();
   }
+
+  // Like toString, but with subsecond time that's included only when
+  // the trailing digits aren't .000.  Always UTC.
+  QString toPrettyString() const {
+    const char* format;
+    if (this->time().msec()) {
+      format = "yyyy-MM-ddTHH:mm:ss.zzzZ";
+    } else {
+      format = "yyyy-MM-ddTHH:mm:ssZ";
+    }
+    return this->toUTC().toString(format);
+  }
+
  private:
   time_t t_;
 };
index 50f71bd18889c52f0e3573b8fe24f20ac063aaf3..13d61e81033e94d8c219948023faf88c022ab865 100644 (file)
@@ -107,38 +107,14 @@ write_xml_entity_end(gbfile *ofd, const QString& indent,
   gbfprintf(ofd, "%s</%s>\n", indent.toAscii().data(), tag.toAscii().data());
 }
 
-void
-xml_fill_in_time(char *time_string, const gpsbabel::DateTime datetime, int long_or_short)
-{
-  QDateTime dt = datetime;
-  dt = dt.toUTC();
-
-  const char* format;
-  if (long_or_short == XML_LONG_TIME) {
-    format = "yyyy-MM-ddTHH:mm:ssZ";
-    if (dt.time().msec()) {
-fprintf(stderr, "this should not be possible");
-abort();
-      format = "yyyy-MM-ddTHH:mm:ss.zzzZ";
-    }
-  } else {
-    format = "yyyyMMddTHHmmssZ";
-  }
-  strcpy(time_string, qPrintable(dt.toString(format)));
-}
-
 void
 xml_write_time(gbfile *ofd, gpsbabel::DateTime dt, const char *elname)
 {
-  char time_string[64];
-  xml_fill_in_time(time_string, dt, XML_LONG_TIME);
-  if (time_string[0]) {
-    gbfprintf(ofd, "<%s>%s</%s>\n",
-              elname,
-              time_string,
-              elname
-             );
-  }
+  gbfprintf(ofd, "<%s>%s</%s>\n",
+            elname,
+            dt.toPrettyString().toUtf8().data(),
+            elname
+           );
 }
 
 /***********************************************************************
index 185a753f838be768d20f4a64001bcb3643773843..6a36414f476c25032ee06a779fac03b4ba7089e1 100644 (file)
@@ -52,7 +52,6 @@ void write_xml_entity_end(gbfile* ofd, const QString& indent, const QString& tag
 void write_optional_xml_entity(gbfile* ofd, const QString& indent,
                                const QString& tag, const QString& value);
 void xml_write_time(gbfile* ofd, gpsbabel::DateTime time, const char* elname);
-void xml_fill_in_time(char* time_string, const gpsbabel::DateTime timep, int long_or_short);
 void write_xml_header(gbfile* ofd);
 void xml_ignore_tags(const char** taglist);